var option = {
    title: {
        text: '广告报表'
    },
    tooltip : {
        trigger: 'axis',
        axisPointer: {
            type: 'cross',
            label: {
                backgroundColor: '#6a7985'
            }
        }
    },
    legend: {
        data:['请求次数','展示次数','点击次数']
    },
    toolbox: {
        feature: {
            saveAsImage: {}
        }
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis : [
        {
            type : 'category',
            boundaryGap : false,
            data : ['周一','周二','周三','周四','周五','周六','周日']
        }
    ],
    yAxis : [
        {
            type : 'value'
        }
    ],
    series : [
        {
            name:'请求次数',
            type:'line',
            stack: '总量',
            areaStyle: {normal: {}},
            data:[120, 132, 101, 134, 90, 230, 210]
        },
        {
            name:'展示次数',
            type:'line',
            stack: '总量',
            areaStyle: {normal: {}},
            data:[220, 182, 191, 234, 290, 330, 310]
        },
        {
            name:'点击次数',
            type:'line',
            stack: '总量',
            areaStyle: {normal: {}},
            data:[220, 182, 191, 234, 290, 330, 310]
        },

    ]
};

var initStartTime = '2017-09-01', initEndTime = '2017-09-10';
var startTime = initStartTime,endTime = initEndTime;
$(function() {
    $('input[name="daterange"]').daterangepicker(
        {
            locale: {
                format: 'YYYY-MM-DD'
            },
            startDate: initStartTime,
            endDate: initEndTime,
            linkedCalendars: false
        },
        function(start, end, label) {
            console.log("A new date range was chosen: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
            startTime = start.format('YYYY-MM-DD');
            endTime = end.format('YYYY-MM-DD');
        }
    );

    var devId = localStorage.id;
    var url = localStorage.url;

    // 加载收入曲线,默认显示1个
    $.ajax({
        type: "get",//请求方式,post\get只要在这里改，其他地方不要改
        url: url+"/boss/developer/adpos/getall",//访问的地址
        data: {did:devId},// 要提交的数据,
        dataType:'json',//传入数据的格式，可以统一用json
        success: function (msg) {
            console.log(msg);
            var allAct=eval(msg);
            var select=$('#selectAdSpace');
            for(var i in allAct){
                var pid=allAct[i].adpositionid;
                var pname=allAct[i].pwebsiteurl;
            // <li><a href="#"><input type="checkbox" name="d-s-c-1">广告位1</a></li>
                // var opt='<option value='+pid+'>'+pname+'</option>';
                var opt = '<li><a href="#"><input type="checkbox" name="check" value='+pid+'>'+pname+'</a></li>'
                select.append(opt);
            }

            var sendData={};
            console.log(select.val());
            //sendData.did=select.val();

            sendData.did= devId;
            sendData.pid=allAct[0].adpositionid;

            console.log(select.val());
            var d = new Date();
            sendData.starttime = '2017-9-1';
            sendData.endtime='2017-9-30';


            $.ajax({
                type: "get",//请求方式,post\get只要在这里改，其他地方不要改
                url: url+"/boss/develop/income/table",//访问的地址
                data: sendData,// 要提交的数据,
                dataType:'json',//传入数据的格式，可以统一用json
                success: function (data) {
                    // var data1=data.data1;
                    // var data2=data.data2;
                    incomeContent.xAxis.data = data.x;
                    incomeContent.series[0].data=data.y;
                    incomeContent.series[0].name=allAct[0].pwebsiteurl;
                    incomeContent.legend.data[0]=allAct[0].pwebsiteurl;
                    // option.xAxis[0].data=data1.x;
                    // option.series[0].data=data1.y1;
                    // option.series[1].data=data1.y2;
                    // option.series[2].data=data1.y3;

                    incomeChart = echarts.init($('#incomeChart')[0]);
                    // 使用刚指定的配置项和数据显示图表。
                    incomeChart.setOption(incomeContent);

                    // $('#dt-ad').DataTable({
                    //     data: data2,
                    //     columns: [
                    //         { title: "日期" },
                    //         { title: "请求次数" },
                    //         { title: "展示次数" },
                    //         { title: "点击次数" },
                    //         { title: "点击率" }
                    //     ]
                    // });

                },
                error: function (error) {

                }
            });
        },
        error:function(error){}
    });


});

var incomeChart;

incomeContent = {
    title: {
        text: '广告收入曲线'
        // subtext: '纯属虚构'
    },
    tooltip: {
        trigger: 'axis'
    },
    legend: {
        data:['广告位1']
    },
    toolbox: {
        show: true,
        feature: {
            dataZoom: {
                yAxisIndex: 'none'
            },
            dataView: {readOnly: false},
            magicType: {type: ['line', 'bar']},
            restore: {},
            saveAsImage: {}
        }
    },
    xAxis:  {
        type: 'category',
        boundaryGap: false,
        data: ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
    },
    yAxis: {
        type: 'value',
        axisLabel: {
            formatter: '￥ {value}k'
        }
    },
    series: [
        {
            name:'广告位1',
            type:'line',
            data:[11, 11, 15, 13, 12, 13, 10, 9, 11, 13, 10, 11],
            markPoint: {
                data: [
                    {type: 'max', name: '最高单月收入'},
                    {type: 'min', name: '最低单月收入'}
                ]
            },
            markLine: {
                data: [
                    {type: 'average', name: '平均值'}
                ]
            }
        }
    ]
};

var seriesCopy = {
    name:'广告位1',
    type:'line',
    data:[11, 11, 15, 13, 12, 13, 10, 9, 11, 13, 10, 11],
    markPoint: {
        data: [
            {type: 'max', name: '最高单月收入'},
            {type: 'min', name: '最低单月收入'}
        ]
    },
    markLine: {
        data: [
            {type: 'average', name: '平均值'}
        ]
    }
};


var portionChart = echarts.init($('#portionChart')[0]);
portionContent = {
    title : {
        text: '各广告位收入占比',
        x:'center'
    },
    tooltip : {
        trigger: 'item',
        formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    legend: {
        orient: 'vertical',
        left: 'left',
        data: ['直接访问','邮件营销','联盟广告','视频广告','搜索引擎']
    },
    series : [
        {
            name: '访问来源',
            type: 'pie',
            radius : '55%',
            center: ['50%', '60%'],
            data:[
                {value:335, name:'直接访问'},
                {value:310, name:'邮件营销'},
                {value:234, name:'联盟广告'},
                {value:135, name:'视频广告'},
                {value:1548, name:'搜索引擎'}
            ],
            itemStyle: {
                emphasis: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }
    ]
};
portionChart.setOption(portionContent);




function changeOption(e) {
    // $('#selectAdSpace').each(function(){
    //     for(var i=0;i<)
    // });
    // var a = $('#selectAdSpace').children().length, b=0;
    // $('#selectAdSpace').children().each(function (i) {
    //     if(this.checked){
    //         b++;
    //     }
    // });
    console.log($(e).children(':first').val());

}

function changeView(e) {
    console.log($(e).prev());
    var choice = $("input[name='check']");
    var adIdSet = [];
    var Url='?did='+localStorage.id;
    console.log(choice);
    var count = 0;
    for(var i = 0,len = choice.length;i<len;i++){
        if(choice[i].checked){
            Url+='&pid='+choice[i].value;
            adIdSet.push(choice[i].value);
            count++;
            console.log(choice[i].value);
        }
    }
    console.log(count);
    console.log(adIdSet);

    // var url = localStorage.url;
    // var sendData = {};
    // sendData.pid = adIdSet;
    // sendData.did = localStorage.id;
    // sendData.starttime = startTime;
    // sendData.endtime=endTime;

    Url+= '&starttime='+startTime+'&endtime='+endTime;

    //console.log(sendData);
    $.ajax({
        type: "get",//请求方式,post\get只要在这里改，其他地方不要改
        url: localStorage.url+"/boss/develop/income/table/array"+Url,//访问的地址
        // data: sendData,// 要提交的数据,
        dataType:'json',//传入数据的格式，可以统一用json
        success: function (data) {
            console.log(data);
            var count = 0;
            //var obj = JSON.parse(data);
            for(var eachOne in adIdSet){
                console.log(eachOne);
                var xaxis = data['x'+adIdSet[eachOne]];
                var yaxis = data['y'+adIdSet[eachOne]];
                console.log(xaxis);
                console.log(yaxis);
                //console.log(obj["x2"]);
                if(xaxis != undefined){
                    console.log(count);
                    incomeContent.xAxis.data = xaxis;
                    seriesCopy.data = yaxis;
                    seriesCopy.name = 1111;
                    incomeContent.series.push(seriesCopy);
                    incomeContent.legend.data.push(1111);
                    count++;
                }
            }

            // // 使用刚指定的配置项和数据显示图表。
            incomeChart.setOption(incomeContent);
            //console.log(data.x1);
            // incomeContent.xAxis.data = data.x;
            // incomeContent.series[0].data=data.y;
            // incomeContent.series[0].name=allAct[0].pwebsiteurl;
            // incomeContent.legend.data[0]=allAct[0].pwebsiteurl;
            // option.xAxis[0].data=data1.x;
            // option.series[0].data=data1.y1;
            // option.series[1].data=data1.y2;
            // option.series[2].data=data1.y3;
            // var incomeChart = echarts.init($('#incomeChart')[0]);
            // // 使用刚指定的配置项和数据显示图表。
            // incomeChart.setOption(incomeContent);

            // $('#dt-ad').DataTable({
            //     data: data2,
            //     columns: [
            //         { title: "日期" },
            //         { title: "请求次数" },
            //         { title: "展示次数" },
            //         { title: "点击次数" },
            //         { title: "点击率" }
            //     ]
            // });

        },
        error: function (error) {

        }
    });
}

